package com.hsmw.api.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hsmw.api.mapper.HsmwOrderVehicleRelationMapper;
import com.hsmw.api.vo.*;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.constant.ChildOrderStatusEnum;
import com.hsmw.domain.*;
import com.hsmw.domain.vo.DriverCountVO;
import com.hsmw.domain.vo.HsmwOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwOrderVehicleRelationService extends BaseService<HsmwOrderVehicleRelationMapper, HsmwOrderVehicleRelation> {

	@Autowired
	private HsmwOrderVehicleRelationMapper hsmwOrderVehicleRelationMapper;


	@Autowired
	private HsmwOrderService hsmwOrderService;


	@Autowired
	private HsmwHtnUserService hsmwHtnUserService;


	//比较并更新
	public boolean compareAndUpdate(HsmwOrderVehicleRelation relation){
		HsmwOrderVehicleRelation hsmwOrderVehicleRelation = hsmwOrderVehicleRelationMapper.selectById(relation.getId());
		if(relation.getVersion() != hsmwOrderVehicleRelation.getVersion() ){
			return false;
		}
		hsmwOrderVehicleRelationMapper.updateById(relation);
		return true;
	}


	public List<DriverOrderInfoVO> getOrderListByParam(HashMap param) {
		return hsmwOrderVehicleRelationMapper.getOrderListByParam(param);
	}

	public List<CurrentOrderInfoVO> getGoingOrderListByParam(HashMap param) {
		return hsmwOrderVehicleRelationMapper.getGoingOrderListByParam(param);
	}

	public List<DriverOrderResultVO> getOrderInfoStatistics(HashMap param) {

		return hsmwOrderVehicleRelationMapper.getOrderInfoStatistics(param);

	}

	public List<DriverCountVO> getChildOrderDrivers(HashMap param) {
		return  hsmwOrderVehicleRelationMapper.getChildOrderDrivers(param);
	}


	public List<OrderManagerDriverVO> getChildDriversInfo(HashMap param) {
		return  hsmwOrderVehicleRelationMapper.getChildDriversInfo(param);
	}

//
	public boolean checkIfVehicleWorking(String vehicleId) {
		List<String> orderStatus = new ArrayList<>();
		orderStatus.add(ChildOrderStatusEnum.RECEIVED.getValue());
		orderStatus.add(ChildOrderStatusEnum.STARTUP.getValue());
		orderStatus.add(ChildOrderStatusEnum.ARRIVED.getValue());
		orderStatus.add(ChildOrderStatusEnum.WORKING.getValue());
		orderStatus.add(ChildOrderStatusEnum.UNDERREVIEW.getValue());
		List<HsmwOrderVehicleRelation> relations = this.list(new LambdaQueryWrapper<HsmwOrderVehicleRelation>().
				in(HsmwOrderVehicleRelation::getOrderStatus,orderStatus).
				eq(HsmwOrderVehicleRelation::getVehicleId,vehicleId));
		if(CollectionUtil.isNotEmpty(relations) && relations.size()>0){
			return false;
		}else {
			return true;
		}
	}

	public RelationsInfo checkIfComplete(String orderId){
		List<HsmwOrderVehicleRelation> relations = list(new LambdaQueryWrapper<HsmwOrderVehicleRelation>()
				.eq(HsmwOrderVehicleRelation::getOrderId,orderId));
		int  size = 0;
		if(CollectionUtil.isNotEmpty(relations)){
			size = relations.size();
		}
		HsmwOrder order = hsmwOrderService.getById(orderId);
		HsmwHtnUser foremanUser =hsmwHtnUserService.getHsmwUserById(order.getCreateUser());
		RelationsInfo info = new RelationsInfo();
		if(size == Integer.parseInt(order.getReqCount())){
			info.setRelations(relations);
			info.setFlag(true);
			info.setForemanOpenId(foremanUser.getOpenId());
		}else {
			info.setFlag(false);
		}
		return  info;
	}


	public List<HsmwOrderVehicleRelation> unSetUplist() {
		return hsmwOrderVehicleRelationMapper.unSetUplist();
	}



	public List<DriverCountVO> getDriversByOrderInfo(HashMap param) {
		return  hsmwOrderVehicleRelationMapper.getDriversByOrderInfo(param);
	}

	public List<DriverOrderResultVO> getCancelOrderInfoStatistics(HashMap param) {

		return hsmwOrderVehicleRelationMapper.getCancelOrderInfoStatistics(param);
	}

    public int getOrderCountByUserId(HashMap param) {
		return hsmwOrderVehicleRelationMapper.getOrderCountByUserId();
    }
}
